package top.sdwu_top.kbase.common.model;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ToString
public class Wrapper<T> implements Serializable {

    /**
     * 成功码.
     */
    public static final int SUCCESS_CODE = 200;

    /**
     * 成功信息.
     */
    public static final String SUCCESS_MESSAGE = "ok";

    /**
     * 错误码.
     */
    public static final int ERROR_CODE = 500;

    /**
     * 错误信息.
     */
    public static final String ERROR_MESSAGE = "内部异常";

    public static final String MICROSERVICE_ERROR = "微服务故障, 请稍后再试";


    /**
     * 编号.
     */
    private int code;

    /**
     * 信息.
     */
    private String message;

    /**
     * 结果数据
     */
    private T result;

    /**
     * Instantiates a new wrapper. default code=200
     */
    public Wrapper() {
        this(SUCCESS_CODE, SUCCESS_MESSAGE);
    }

    public Wrapper(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Wrapper(int code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    /**
     * Wrap.
     *
     * @param code    the code
     * @param message the message
     * @return the wrapper
     */
    public static <T> Wrapper<T> wrap(int code, String message, T t) {
        return new Wrapper<T>(code, message, t);
    }

    /**
     * Wrap.
     *
     * @param code    the code
     * @param message the message
     * @return the wrapper
     */
    public static <T> Wrapper<T> wrap(int code, String message) {
        return wrap(code, message, null);
    }

    public static <T> Wrapper<T> ok() {
        return wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }

    public static <T> Wrapper<T> ok(String message) {
        return wrap(Wrapper.SUCCESS_CODE, message);
    }

    public static <T> Wrapper<T> data(T t) {
        return wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, t);
    }

    public static <T> Wrapper<T> ok(T t) {
        return new Wrapper<>(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, t);
    }

    public static <T> Wrapper<T> ok(String message, T t) {
        return new Wrapper<>(Wrapper.SUCCESS_CODE, message, t);
    }

    /**
     * Wrap ERROR. code=500
     *
     * @return the wrapper
     */
    public static <T> Wrapper<T> error() {
        return wrap(Wrapper.ERROR_CODE, Wrapper.ERROR_MESSAGE);
    }

    /**
     * Wrap ERROR. code=500
     *
     * @return the wrapper
     */
    public static <T> Wrapper<T> error(String errorMessage) {
        return wrap(Wrapper.ERROR_CODE, errorMessage);
    }

    /**
     * Wrap ERROR. code=500
     *
     * @return the wrapper
     */
    public static <T> Wrapper<T> microserviceError() {
        return wrap(Wrapper.ERROR_CODE, Wrapper.MICROSERVICE_ERROR);
    }
}